# -*- coding:utf-8 -*-
from copy import deepcopy


# 递归获取分类树(把所有的数据查出来，数据处理成树）
def get_tree(source_list):
    """
    递归获取所有层级菜单
    @param source_list:[dict(item) for item in serializer.data]
    """

    def get_children_tree(sources, source_item):
        for source in sources:
            if source['id'] == source_item['parentId']:
                children = source.get('children', [])
                if not children:
                    source['children'] = []
                    source['children'].append(source_item)
                if children:
                    source['children'].append(source_item)
            else:
                if source.get('children', []):
                    get_children_tree(source['children'], source_item)

    result_tree = list(source_list)

    for item in source_list:
        if item['parentId']:
            result_tree.remove(item)
            get_children_tree(result_tree, item)

    return result_tree


"""
def list(self, request, *args, **kwargs):
    # queryset = self.filter_queryset(self.get_queryset())
    queryset = Menu.objects.all()
    serializer = self.get_serializer(queryset, many=True)

    # 获取所有的菜单项
    menu_list = [dict(item) for item in serializer.data]

    return SuccessResponse(get_tree(menu_list))
"""


# 通过一个ID，把树中对应的子ID都查出来
def get_all_children(source_list, source_id):
    def get_children_tree(sources, source_item):
        pass
    result_id_list = [source_id]
    for item in source_list:
        pass
    return result_id_list
